import wtforms
from wtforms.validators import Email, Length, EqualTo, input_required, NumberRange, ValidationError
from models import UserModel
from datetime import datetime
# from flask_wtf import FlaskForm,CSRFProtect
from wtforms import Form, DateField
from datetime import date, datetime

class RegisterForm(Form):
    # email = wtforms.StringField(validators=[Email(message="邮箱格式错误")])
    username = wtforms.StringField(validators=[Length(min=2,max=20,message="用户名格式错误")])
    password = wtforms.StringField(validators=[Length(min=4,max=20,message="密码格式错误")])
    password_confirm = wtforms.StringField(validators=[EqualTo("password")])
    # wechat = wtforms.StringField(validators=[Length(min=6,max=20,message="微信账号格式错误")])

    # def validate_username(self, field):
    #     username = field.data
    #     user = Usermodel.query.filter_by(username=username).first()
    #     if user:
    #         raise wtforms.ValidationError(message="该用户名已经被注册")



# email = self.email.data

class LoginForm(Form):
    # email = wtforms.StringField(validators=[Email(message="邮箱格式错误")])
    username = wtforms.StringField(validators=[Length(min=2,max=20,message="用户名格式错误")])
    password = wtforms.StringField(validators=[Length(min=4,max=20,message="密码格式错误")])

class CancenlationForm(Form):
    username = wtforms.StringField(validators=[Length(min=2, max=20, message="用户名格式错误")])
    # password = wtforms.StringField(validators=[Length(min=4, max=20, message="密码格式错误")])

class InsertForm(Form):
    objectname = wtforms.StringField(validators=[Length(min=1, max=20, message="物品名称格式错误")])
    count = wtforms.IntegerField(validators=[NumberRange(min=1, max=100000000, message = '数字大小错误')])
    classfication = wtforms.StringField(validators=[Length(min=0,max=100, message="分类名称格式错误")])
    price = wtforms.IntegerField(validators=[NumberRange(min=1, max=100000, message='数字大小错误')])
    picture = wtforms.StringField(validators=[Length(min=0, max=10000000, message="图片格式错误")])

    place = wtforms.StringField(validators=[Length(min=0, max=20, message="地点格式错误")])

    remarks = wtforms.StringField(validators=[Length(min=0, max=100, message="备注格式错误")])

    date = DateField('Date', format='%Y-%m-%d')
    def validate_data(self, field):
        if field.data > date.today():
            raise ValidationError('Date must be before today.')


class DeleteForm(Form):
    objectname = wtforms.StringField(validators=[Length(min=1,max=20,message="物品名称格式错误")])


class UpdateForm(Form):
    objectname = wtforms.StringField(validators=[Length(min=1,max=20,message="物品名称格式错误")])
    count = wtforms.IntegerField(validators=[NumberRange(min=1, max = 100000000, message = '数字大小错误')])
    classfication = wtforms.StringField(validators=[Length(min=0,max=100,message="分类名称格式错误")])
    # price = wtforms.IntegerField(validators=[NumberRange(min=1, max = 100000, message = '数字大小错误')])
    # maturity_time = wtforms.DateTimeField(validators=[])
    picture = wtforms.StringField(validators=[Length(min=0,max=10000000,message="图片格式错误")])

    place = wtforms.StringField(validators=[Length(min=0,max=20,message="地点格式错误")])

    remarks = wtforms.StringField(validators=[Length(min=0,max=100,message="备注格式错误")])


class Get_object_idForm(Form):
    objectname = wtforms.StringField(validators=[Length(min=1,max=20,message="物品名称格式错误")])
    user_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="用户id格式错误")])


class SelectForm(Form):
    object_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="物品id格式错误")])
    user_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="用户id格式错误")])

class Fl_showForm(Form):
    user_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="用户id格式错误")])
    classfication = wtforms.StringField(validators=[Length(min=0, max=100, message="分类名称格式错误")])


class ShowForm(Form):
    user_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="用户id格式错误")])

class YmForm(Form):
    user_id = wtforms.IntegerField(validators=[NumberRange(min=1,max=100000000,message="用户id格式错误")])
    # def validate_maturity_time(self, field):
    #     maturity_time = str(field.data)
    #     print(maturity_time)
    #     year = datetime.now().timetuple().tm_year
    #     month = datetime.now().timetuple().tm_mon
    #     if int(maturity_time[0:4]) > year or (int(maturity_time[0:4]) == year and int(maturity_time[5:7]) > month):
    #         raise wtforms.ValidationError(message="查找时间错误")


